package com.example.mntz.util;

import com.example.mntz.dao.TzHttpUrlMapper;
import com.example.mntz.model.TzHttpUrl;
import com.example.mntz.model.TzXk;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.NumberFormat;

@Component
public  class   TzUtil {
    /*投资API访问*/
    public  static   String  TzUtilGet(String dama,TzHttpUrl httpUrl) throws IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Get请求
        HttpGet httpGet = new HttpGet(httpUrl.getPrefix()+dama+httpUrl.getSuffix());

        //创建输出对象
        CloseableHttpResponse response =null;
        String content = null;

        try {
            response = httpClient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 解析响应，获取数据
                content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } finally {
            if(response!=null){
                response.close();
            }
            httpClient.close();
            return content;
        }
    }
    /*股票*/
    public static TzXk GpTzXk(String var){
        int i1 = (var.indexOf("\""));
        String  s = var.substring(i1+1);
        String[] strings = s.split(",");
        TzXk tzXk = new TzXk();
        try {
            tzXk.setName(strings[0]);
            tzXk.setDate(strings[30]+" "+strings[31]);
            tzXk.setLb("股票");
            double j = Double.parseDouble(strings[3]);
            double z = Double.parseDouble(strings[2]);
            double zf = (z-j)/z;
            tzXk.setZr(strings[2]);
            tzXk.setZf(TzUtil.bfz(String.valueOf(zf)));
            tzXk.setJr(strings[3]);
        }catch (Exception e){
            System.out.println("股票参数出错");
        }
        return tzXk;


    }
    /*基金*/
    public static TzXk JjTzXk(String var){
        String[] strings = var.split("\"");
        TzXk tzXk = new TzXk();
        try{

            tzXk.setDate(strings[27]);
            tzXk.setLb("基金");
            tzXk.setName(strings[7]);
            tzXk.setZr(strings[15]);
            tzXk.setZf(TzUtil.bfz(strings[23]));
            tzXk.setJr(strings[19]);
        }catch (Exception e){
            System.out.println("基金参数出错");
        }
        return tzXk;

    }
    /*涨幅百分制*/
    public static String bfz(String shu){
        Double d = Double.valueOf(shu);
        NumberFormat nf = java.text.NumberFormat.getPercentInstance();
        nf.setMaximumIntegerDigits(2);//小数点前保留几位
        nf.setMinimumFractionDigits(2);// 小数点后保留几位
        String str = nf.format(d);
        return str;
    };
    /*金价*/
    public static TzXk JzTzXk(String var){
        String[] strings = var.split("\"");
        TzXk tzXk = new TzXk();
        tzXk.setDate(strings[47]);
        tzXk.setCode("Au100g");
        tzXk.setLb("金价");
        tzXk.setName("Au100g");
        tzXk.setZr(strings[39]);
        tzXk.setZf(strings[35]);
        tzXk.setJr(strings[19]);
        return tzXk;
    }

}
